/*
 * Copyright 2007 Seok-Ho, Yang
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package kr.javanese.devtools.m2wtp.builder;

import java.util.HashSet;
import java.util.Set;

import kr.javanese.devtools.m2wtp.common.M2WTPConstants;

import org.eclipse.core.resources.ICommand;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.IProjectNature;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.maven.ide.eclipse.Maven2Plugin;

/**
 * Class which configure M2WTP nature for a given project.
 * 
 * @author Seok-Ho, Yang (KR)
 * @author Cyril Joui (FR)
 */
public class M2WTPProjectNature implements IProjectNature {

	/**
	 * Unique ID of this project nature.
	 */
	public static final String NATURE_ID = "kr.javanese.devtools.m2wtp.m2wtpNature";

	/**
	 * Given project.
	 */
	private IProject project;


	/**
	 * Configure method (from parent interface).
	 */
	public void configure() throws CoreException {
		IProjectDescription desc = project.getDescription();
		ICommand[] commands = desc.getBuildSpec();

		for (int i = 0; i < commands.length; ++i) {
			if (commands[i].getBuilderName().equals(WTPModuleDependencyBuilder.BUILDER_ID)) {
				return;
			}
		}

		ICommand[] newCommands = new ICommand[commands.length + 1];
		System.arraycopy(commands, 0, newCommands, 0, commands.length);
		ICommand command = desc.newCommand();
		command.setBuilderName(WTPModuleDependencyBuilder.BUILDER_ID);
		newCommands[newCommands.length - 1] = command;
		desc.setBuildSpec(newCommands);
		project.setDescription(desc, null);

		// add Web Application Libraries (if not added before).
		IJavaProject javaProject = JavaCore.create(getProject());
		IClasspathEntry[] classpathEntries = javaProject.getRawClasspath();
		Set<IClasspathEntry> newClasspathEntries = new HashSet<IClasspathEntry>();
		boolean addWebLibContainer = true;
		for (IClasspathEntry classpathEntry : classpathEntries) {
			if (M2WTPConstants.WTP_WEBLIB_CONTAINER_ID
					.equals(classpathEntry.getPath().toString())) {
				// web app lib already added ?
				addWebLibContainer = false;
			} else if (M2WTPConstants.MAVEN2PLUGIN_CONTAINER_ID
					.equals(classpathEntry.getPath().toString())) {
				// maven dependencies path ?
				// don't add it
				continue;
			}
			// add this path to new classpath
			newClasspathEntries.add(classpathEntry);
		}
		if (addWebLibContainer) {
			newClasspathEntries.add(JavaCore.newContainerEntry(
					new Path(M2WTPConstants.WTP_WEBLIB_CONTAINER_ID),
			false));
		}
		System.out.println("newClasspathEntries: " + newClasspathEntries);

		// set new classpath to java project
		javaProject.setRawClasspath(newClasspathEntries.toArray(new IClasspathEntry[0]),
				new NullProgressMonitor());
		// launch first process (current pom file)
		PomProcessor pomProcessor = new PomProcessor();
		IFile pomFilePath = project.getFile(new Path(Maven2Plugin.POM_FILE_NAME));
		System.out.println("pomFilePath: " + pomFilePath);
		if (pomFilePath != null) {
			pomProcessor.process(pomFilePath,
					new NullProgressMonitor());
		}
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see org.eclipse.core.resources.IProjectNature#deconfigure()
	 */
	public void deconfigure() throws CoreException {
		IProjectDescription description = getProject().getDescription();
		ICommand[] commands = description.getBuildSpec();
		for (int i = 0; i < commands.length; ++i) {
			if (commands[i].getBuilderName().equals(WTPModuleDependencyBuilder.BUILDER_ID)) {
				ICommand[] newCommands = new ICommand[commands.length - 1];
				System.arraycopy(commands, 0, newCommands, 0, i);
				System.arraycopy(commands, i + 1, newCommands, i,
						commands.length - i - 1);
				description.setBuildSpec(newCommands);
				return;
			}
		}
	}

	/**
	 * @return the project
	 */
	public IProject getProject() {
		return project;
	}

	/**
	 * @param project the project to set
	 */
	public void setProject(IProject project) {
		this.project = project;
	}
}
